package captcha

import (
	"fmt"
	"github.com/mojocn/base64Captcha"
)

var store = base64Captcha.DefaultMemStore

// 获取验证码
func GetCaptchaAndId(len int) map[string]string {
	stringDriver := getCaptchaStringDriver(len)
	captcha := base64Captcha.NewCaptcha(stringDriver, store)
	id, b64s, err := captcha.Generate()
	if err != nil {
		fmt.Println(err)
	}
	detail := make(map[string]string, 2)
	detail["captchaId"] = id
	detail["img"] = b64s
	return detail
}

// 验证码验证
func Verify(captchaId string, captcha string) bool {
	if captchaId == "" || captcha == "" {
		return false
	}
	return store.Verify(captchaId, captcha, true)
}

// 获取字符串类型的验证码图片
func getCaptchaStringDriver(len int) *base64Captcha.DriverString {
	captcha_config := base64Captcha.DriverString{
		Height:          60,
		Width:           200,
		NoiseCount:      0,     //噪点
		ShowLineOptions: 0 | 1, //干扰线
		Length:          len,
		Source:          "1234567890abcdefghijklmnopqrstuvwxyz",
	}
	driver := captcha_config.ConvertFonts()
	return driver
}
